<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" left-arrow @click-left="$router.back()" />
    <!-- /导航栏 -->

    <input 
    type="file" 
    hidden 
    ref="file"
    @change="onFileChange"
    >

    <!-- 个人信息 -->
    <van-cell title="头像" 
     @click="$refs.file.click()"
    is-link>
      <van-image 
      class="avatar" 
      fit="cover" 
      :src="user.photo" 
      />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isUpdateNameShow = true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isUpdateGenderShow = true"
    />
    <van-cell title="生日" 
    :value="user.birthday"
     is-link
     @click="isUpdateBirthdayShow=true"
      />
    <!-- /个人信息 -->

    <!-- 编辑昵称 -->
    <van-popup
      v-model="isUpdateNameShow"
      style="height: 100%"
      position="bottom"
    >
      <UpdateName
        v-if="isUpdateNameShow"
        @close="isUpdateNameShow = false"
        v-model="user.name"
      />
    </van-popup>
    <!-- /编辑昵称 -->

    <!-- 编辑性别 -->
    <van-popup v-model="isUpdateGenderShow"
     position="bottom">
      <UpdateGender
        v-if="isUpdateGenderShow"
        @close="isUpdateGenderShow = false"
        v-model="user.gender"
      />
    </van-popup>
    <!-- /编辑性别 -->

    <!-- 编辑生日 -->
     <van-popup
      v-model="isUpdateBirthdayShow"
      position="bottom">
      <UpdateBirthday
      v-if="isUpdateBirthdayShow"
       @close="isUpdateBirthdayShow = false"
       v-model="user.birthday"
      />
     </van-popup>
    <!-- /编辑生日 -->

    <!-- 编辑头像 -->
    <!-- 编辑生日 -->
     <van-popup
      v-model="isUpdatePhotoShow"
      position="bottom"
      style="height:100%"
      >
     <UpdatePhoto
     v-if="isUpdatePhotoShow"
     @close='isUpdatePhotoShow=false'
     :img="img"
     @update-photo="user.photo = $event"
     />
     </van-popup>
    <!-- /编辑头像 -->


  </div>
</template>

<script>
import { getUserProfile } from "@/api/user";
import UpdateName from "./components/update-name.vue";
import UpdateGender from "./components/update-gender.vue";
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'

export default {
  name: "UserProfile",
  data() {
    return {
      user: {}, //用户信息
      isUpdateNameShow: false, //修改昵称弹出框
      isUpdateGenderShow: false,
      isUpdateBirthdayShow:false,
      isUpdatePhotoShow:false,
      img:null,//预览图片
    };
  },
  components: { UpdateName, UpdateGender ,UpdateBirthday,UpdatePhoto},
  created() {
    this.loadUserProfile();
  },
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile();
        this.user = data.data;
        console.log(data);
      } catch (error) {
        this.$toast.fail("获取用户信息失败");
      }
    },
    onFileChange(){
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文章对象 获取blob数据
      const data = window.URL.createObjectURL(file)
      console.log(data);
      this.img = data
      this.isUpdatePhotoShow = true

      // file-input选了同一个文件不会触发
      // 每次使用完毕 value清空
      this.$refs.file.value = ''
    }
  },
};
</script>

<style lang="less" scoped>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>